package com.ninth.element.generated.number.generate.mysql;

import com.ninth.element.generated.number.generate.common.pojo.enums.ResetTypeEnum;
import com.ninth.element.generated.number.generate.fallback.DefaultIdFallback;
import com.ninth.element.generated.number.generate.fallback.IdFallback;
import lombok.Data;
import org.springframework.util.Assert;

import java.time.format.DateTimeFormatter;

/**
 * @author LiYUJi
 **/
@Data
public class MysqlIdPatternConfiguration {

    /**
     * 序号类型字符串-如：拼接的字符串  如果不设置则为null，则不区分类型 -- 目前已弃用
     */
    private String serialNumType;
    /**
     * mysql 作为ID前缀（若配置了需要前缀）
     */
    private String key;
    /**
     * 拼接日期格式，为空编号则不拼接日期，默认null
     */
    private DateTimeFormatter dateTimeFormatter;
    /**
     * mysql id位数，id不足位数则在前面补0，默认0
     */
    private Integer digits;
    /**
     * mysql id增长步长，默认1
     */
    private Integer increment;
    /**
     * mysql id初始化值，默认从0开始
     */
    private Long initial;
    /**
     * 是否需要前缀，默认false不拼接
     */
    private boolean needPrefix;
    /**
     * 是否需要重置序列，默认false不重置，为true的话序列会根据重置类型重置为initial的参数值，如果参数类型没有指定则 默认为每天
     */
    private boolean isResetDaily;
    /**
     * 重置类型 ResetTypeEnum 默认为 每天
     */
    private ResetTypeEnum resetType;
    /**
     * mysql不可用时，服务降级的ID生成策略
     */
    private IdFallback idFallback;


    public MysqlIdPatternConfiguration(String serialNumType, String key, DateTimeFormatter dateTimeFormatter, Integer digits, Integer increment, Long initial, boolean needPrefix, boolean isResetDaily, ResetTypeEnum resetType, IdFallback idFallback) {
        this.serialNumType = serialNumType;
        this.key = key;
        this.dateTimeFormatter = dateTimeFormatter;
        this.digits = digits;
        this.increment = increment;
        this.initial = initial;
        this.needPrefix = needPrefix;
        this.isResetDaily = isResetDaily;
        this.resetType = resetType;
        this.idFallback = idFallback;
    }

    public static MysqlIdPatternConfigurationBuilder builder() {
        return new MysqlIdPatternConfigurationBuilder();
    }

    public static class MysqlIdPatternConfigurationBuilder {
        String serialNumType;
        String key;
        DateTimeFormatter dateTimeFormatter;
        Integer digits = 0;
        Integer increment = 1;
        Long initial = 1L;
        boolean needPrefix = false;
        boolean isResetDaily = false;
        ResetTypeEnum resetType = ResetTypeEnum.DAY;
        IdFallback idFallback = new DefaultIdFallback();

        public MysqlIdPatternConfigurationBuilder() {
        }


        public MysqlIdPatternConfigurationBuilder(String serialNumType, String key, DateTimeFormatter dateTimeFormatter) {
            this.serialNumType = serialNumType;
            this.key = key;
            this.dateTimeFormatter = dateTimeFormatter;
        }

        public MysqlIdPatternConfigurationBuilder(String serialNumType, String key, DateTimeFormatter dateTimeFormatter, Integer digits, Integer increment, Long initial, boolean needPrefix, boolean isResetDaily, ResetTypeEnum resetType, IdFallback idFallback) {
            this.serialNumType = serialNumType;
            this.key = key;
            this.dateTimeFormatter = dateTimeFormatter;
            this.digits = digits;
            this.increment = increment;
            this.initial = initial;
            this.needPrefix = needPrefix;
            this.isResetDaily = isResetDaily;
            this.resetType = resetType;
            this.idFallback = idFallback;
        }

        public MysqlIdPatternConfigurationBuilder(String serialNumType, String key, DateTimeFormatter dateTimeFormatter, boolean needPrefix, boolean isResetDaily) {
            this.serialNumType = serialNumType;
            this.key = key;
            this.dateTimeFormatter = dateTimeFormatter;
            this.needPrefix = needPrefix;
            this.isResetDaily = isResetDaily;
        }

        public MysqlIdPatternConfigurationBuilder serialNumType(String serialNumType) {
            Assert.notNull(serialNumType, "serialNumType must not be null!");
            this.serialNumType = serialNumType;
            return this;
        }


        public MysqlIdPatternConfigurationBuilder key(String key) {
            Assert.notNull(key, "key must not be null!");
            this.key = key;
            return this;
        }

        public MysqlIdPatternConfigurationBuilder dateTimeFormatter(DateTimeFormatter dateTimeFormatter) {
            this.dateTimeFormatter = dateTimeFormatter;
            return this;
        }

        public MysqlIdPatternConfigurationBuilder digits(Integer digits) {
            this.digits = digits;
            return this;
        }

        public MysqlIdPatternConfigurationBuilder increment(Integer increment) {
            this.increment = increment;
            return this;
        }

        public MysqlIdPatternConfigurationBuilder initial(Long initial) {
            this.initial = initial;
            return this;
        }

        public MysqlIdPatternConfigurationBuilder needPrefix(Boolean needPrefix) {
            this.needPrefix = needPrefix;
            return this;
        }

        public MysqlIdPatternConfigurationBuilder isResetDaily(Boolean isResetDaily) {
            this.isResetDaily = isResetDaily;
            return this;
        }

        public MysqlIdPatternConfigurationBuilder resetType(ResetTypeEnum resetType) {
            this.resetType = resetType;
            return this;
        }

        public MysqlIdPatternConfigurationBuilder idFallback(IdFallback idFallback) {
            this.idFallback = idFallback;
            return this;
        }

        public MysqlIdPatternConfiguration build() {
            return new MysqlIdPatternConfiguration(this.serialNumType, this.key, this.dateTimeFormatter, this.digits, this.increment, this.initial, this.needPrefix, this.isResetDaily, this.resetType, this.idFallback);
        }
    }
}
